import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/clubs/clubs_controller.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';

/// 切换俱乐部
class SwitchClubDialog {

  static void show(
    BuildContext mContext, {
      Function? switchTap,
      Function? createTap
    }
  ) {
    showDialog(
      // barrierDismissible: false,
      context: mContext,
      builder: (BuildContext context) {
        return ShowDialogPage(
          createTap: createTap,
          switchTap: switchTap
        );
      }
    );
  }

}




class ShowDialogPage extends Dialog {
  ShowDialogPage({
    this.switchTap,
    this.createTap,
  });

  // 切换
  final Function? switchTap;
  // 创建
  final Function? createTap;

  @override
  Widget build(BuildContext dContext) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      //自定义dialog布局
      child: GetBuilder<ClubsController>(
        builder: (clubsController) {
          return Center(
            child: RichText(
              text: TextSpan(
                children: [
                  WidgetSpan(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 40.w),
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12.r)
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            alignment: Alignment.center,
                            height: 48.w,
                            decoration: BoxDecoration(
                              gradient: const LinearGradient(
                                begin: Alignment.topCenter,
                                end: Alignment.bottomCenter,
                                colors: [
                                  Color(0xFFEAF9EF),
                                  Color(0x00EAF9EF),
                                ],
                                stops: [0.0, 1.0],
                              ),
                              borderRadius: BorderRadius.circular(12.r)
                            ),
                            child: Text("切换俱乐部", style: TextStyle(fontSize: 17.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333), ), textAlign: TextAlign.center,),
                          ),

                          SizedBox(height: 12.w,),

                          SizedBox(
                            child: ListView.separated(
                              itemCount: clubsController.clubList.length,
                              shrinkWrap: true,
                              physics: const BouncingScrollPhysics(),
                              padding: EdgeInsets.symmetric(horizontal: 15.w),
                              separatorBuilder: (context, index) {
                                return SizedBox(height: 12.w);
                              },
                              itemBuilder: (BuildContext context, int index) {
                                return GestureDetector(
                                  behavior: HitTestBehavior.translucent,
                                  onTap: () {
                                    if (clubsController.clubList[index].isDefault.toString().toLowerCase() == 'true'.toLowerCase()) {
                                       Get.back();
                                      return ;
                                    }
                                    Get.back();
                                    clubsController.toggleDefaultClub('${clubsController.clubList[index].clubId}');
                                  },
                                  child: Container(
                                    padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w),
                                    decoration: BoxDecoration(
                                      color: index == 0 ? const Color(0xff499360).withOpacity(0.1) : Colors.white,
                                      borderRadius: BorderRadius.circular(8.r)
                                    ),
                                    child: Row(
                                      children: [
                                        Container(
                                          width: 48.w,
                                          height: 48.w,
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(8.r)
                                          ),
                                          child: ClipRRect(
                                            borderRadius: BorderRadius.circular(8.r),
                                            child: Image.network(clubsController.clubList[index].clubImage ?? '', fit: BoxFit.fill,),
                                          ),
                                        ),
                                        SizedBox(width: 8.w,),
                                        Expanded(
                                          child: Text(clubsController.clubList[index].clubName ?? '', style: TextStyle(fontSize: 14.sp, color: index == 0 ? const Color(0xff499360) : const Color(0xff333333), fontWeight: FontWeight.w500),),
                                        )
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),

                          SizedBox(height: 12.w,),

                          if (clubsController.clubList.length < 3) ...[
                            Container(
                              padding: EdgeInsets.only(left: 15.w, right: 15.w, bottom: 16.w),
                              child: GestureDetector(
                                behavior: HitTestBehavior.translucent,
                                onTap: () {
                                  createTap?.call();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w),
                                  decoration: BoxDecoration(
                                    color:const Color(0xffF8F8F8),
                                    borderRadius: BorderRadius.circular(8.r)
                                  ),
                                  child: Row(
                                    children: [
                                      Container(
                                        width: 48.w,
                                        height: 48.w,
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(8.r)
                                        ),
                                        child: Image.asset('assets/images/club_add.png', fit: BoxFit.fill,),
                                      ),
                                      SizedBox(width: 8.w,),
                                      Expanded(
                                        child: Text('新增俱乐部', style: TextStyle(fontSize: 14.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            )
                          ] else ...[
                            SizedBox(height: 20.w,)
                          ]
                        ],
                      ),
                    )
                  ),
                ]
              ),
            ),
          );
        }
      )
    );
  }
}

